<template>
	<div class="right-box">
		<Breadcrumb style="margin-bottom: 5px;">
	        <BreadcrumbItem>
	            <Icon type="ios-archive" color="red" size="18"></Icon> 仓库作业
	        </BreadcrumbItem>
	        <BreadcrumbItem>
	           出库管理
	        </BreadcrumbItem>
	    </Breadcrumb>
	     <Button class="addBtn" @click="addOutModel" v-if="userType==0">新增出库</Button>
	    <Input search enter-button="搜索" placeholder="请输入仓库名..." class="search-input" v-model="search" v @on-search="goSearch"/>
	    <Table height="480" :columns="columns1" :data="datas">
	    	<!--<template slot-scope="{ row, index }" slot="action">
	            <Button type="primary" style="margin-right: 5px" @click="edit(row)"  v-if="userType==0">修改</Button>
	            <Button type="error" @click="remove(row)">删除</Button>
	        </template>-->
	    </Table>
	    <Modal
        title="请按要求完善表单"
        v-model="modelShow"
        :closable="false" @on-ok="checkSure" @on-cancel="handleReset('formValidate')" :styles="{top: '20px'}">
	        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
		        <FormItem label="物品名称" prop="name">
		            <Select v-model="formValidate.name">
		                <Option :value="list.itemId" v-for="(list,index) in files" :key="index">{{list.itemName}}</Option>
		            </Select>
		        </FormItem>
		        <FormItem label="数量" prop="count">
		        	<InputNumber :min="1" v-model="formValidate.count"></InputNumber>
		        </FormItem>
		        <FormItem label="经手人" prop="outItemBrokerage">
		            <Input v-model="formValidate.outItemBrokerage" placeholder="请输入经手人"></Input>
		        </FormItem>
		        <FormItem label="出库部门" prop="department">
		            <Select v-model="formValidate.department">
		                <Option :value="list.departmentId" v-for="(list,index) in departmentList" :key="index">{{list.departmentName}}</Option>
		            </Select>
		        </FormItem>
		    </Form>
	    </Modal>
	</div>
</template>

<script>
	export default{
		name:'outManager',
		data(){
			return{
				files:[],
				departmentList:[],
				userType:null,
				modelShow:false,//弹窗默认隐藏
				formValidate: {
                    name: '',//物品名字
                    department:'',//部门
                    count:1,//数量
                    outItemBrokerage:'',//经手人
                },
                ruleValidate: {//表单校验规则
                    department: [
                        { required: true, message: '请选择部门', trigger: 'blur' }
                    ],
                    file: [
                        { required: true, message: '请选择物品', trigger: 'blur' }
                    ],
                    outItemBrokerage: [
                        { required: true, message: '经手人不能为空', trigger: 'blur' }
                    ],
                },
				columns1: [//表头
                    {
                        title: '物品编号',
                        key: 'outItemId'
                    },
                    {
                        title: '物品名称',
                        key: 'outItemName'
                    },
                    {
                        title: '供应商名称',
                        key: 'outSuppliersName'
                    },
                    {
                    	title:'所属仓库',
                    	key:'outWarehouseName'
                    },
                    {
                    	title:'出库部门',
                    	key:'outDepartmentTargetName'
                    },
                    {
                        title: '数量',
                        key: 'outItemCount'
                    },
                    {
                        title: '日期',
                        key: 'createTime'
                    }
                ],
				datas:[],//表格数据
				search:''
			}
		},
		methods:{
			addOutModel(){//新增出库
				this.modelShow = true
			},
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            handleSubmit(name) {//验证表单
               this.$refs[name].validate((valid) => {
                    if (valid) {//验证通过
                    	this.addOut()
                    } else {//验证不通过
                        this.$Message.error('请按要求完善表单!');
                        this.handleReset('formValidate')
                    }
                })
            },
			checkSure(){//点击弹窗确定 验证表单
				this.handleSubmit('formValidate')
			},
			goSearch(){//搜索方法 点击搜索、或者按回车键都能触发
				if(this.search == ''){this.getOut;return}
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/selectOutWarehouseHistory',
	    			data:{
	    				outWarehouseName:that.search
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.datas = res.data.historyList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			getOut(){//获取出库历史记录
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getOutWarehouseHistory',
	    			data:{
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.datas = res.data.historyList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			addOut(){//物品入库 == 新增物资
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/outWarehouse',
	    			data:{
	    				outItemId:that.formValidate.name,
	    				departmentTargetId:that.formValidate.department,
	    				outItemBrokerage:that.formValidate.itemBrokerage,
	    				outItemCount:that.formValidate.count
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.getOut()
	    				this.$Message.success(res.data.msg);
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			getItem(){//获取所有物资记录
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getItem',
	    			data:{
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.files = res.data.itemList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			getDepartment(){ // 获取部门列表
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getDepartment',
	    			data:{
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.departmentList = res.data.departmentList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			}
		},
		created(){
			this.userType=this.LS.getLS('userType')
			this.getItem()
			this.getDepartment()
			this.getOut()
		}
	}
</script>

<style scoped>
.right-box{
	width: 100%;
	height: 100%;
	position: relative;
}
.addBtn{
	position: absolute;
	top: -10px;
	right: 20px;
}
.page-nav{
	width: 100%;
	display: -webkit-flex;
	display: -ms-flexbox;
	display: -moz-box;
	display: flex;
	justify-content: center;
	align-content: center;
	margin-top: 10px;
}
.search-input{
	width:40%;
	position: absolute;
	top: -10px;
	left: 50%;
	transform: translateX(-50%);
	-webkit-transform: translateX(-50%);
}
</style>